package com.sxkj.service.sys;

import com.sxkj.entity.entity.sys.Menu;
import com.sxkj.entity.entity.sys.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @author: daixiaowei
 * @date: 2019年12月10日 下午 02:43:47
 * @description: SpringSecurity 需要的用户详情
 */
public class AdminUserDetails implements UserDetails {
    public User user;
    public List<Menu> permissionList;
    public AdminUserDetails(User user, List<Menu> permissionList) {
        this.user = user;
        this.permissionList = permissionList;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //返回当前用户的权限
        return permissionList.stream()
                .filter(permission -> permission.getPermission()!=null)
                .map(permission ->new SimpleGrantedAuthority(permission.getPermission()))
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return user.getDelFlag().equals(0);
    }
}
